import torchvision
from torchvision import transforms
import cv2
import PIL
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

"""
不同点：
1. opencv读取格式  BGR
    matplotlib、PIL、torchvision读取格式 RGB
2. Opencv、matplotlib、PIL  图像数组格式是HWC
    torchvision  图像数组格式是CHW
"""

# --------------------------------------------------------------
# PIL的学习
img = Image.open('../../images/lena.png')
# img.show()
img_np = np.array(img)
print(img_np.shape)
img_tensor = transforms.ToTensor()(img)
print(img_tensor.shape)
img = transforms.ToPILImage(mode='RGB')(img_tensor)
img.show()

# --------------------------------------------------------------
img = cv2.imread('../../images/lena.png')
print(img.shape)
img_tensor = transforms.ToTensor()(img)
print(img_tensor.shape)
img_np = img_tensor.permute(1, 2, 0).numpy()
cv2.imshow('img', img_np)
cv2.waitKey(0)

# --------------------------------------------------------------
# torchvision本身一般不直接读取图片，而是借助PIL的Image读取
from torchvision import transforms as T
img = Image.open('../../images/lena.png')
transform = T.Compose([
    T.Resize((100, 100), antialias=True),  # antialias 抗锯齿
    T.Grayscale()
])
img_new = transform(img)
img_new.show()

# --------------------------------------------------------------
img = plt.imread('../../images/lena.png')